package org.web.locks.databasedistributedlocks.sdk.helper;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.UUID;

public class ProcessIdentifierHelper {

    // 缓存进程ID
    private static String cachedProcessId;

    // 缓存主机名
    private static String cachedHostName;

    // 缓存默认IP地址
    private static String cachedDefaultIpAddress;

    // 缓存指定网卡的IP地址
    private static String cachedNetworkInterfaceIpAddress;

    //随机生产一个UUID，作为进程的唯一标识。
    private static final String UUID = java.util.UUID.randomUUID().toString();

    /**
     * 获取当前进程的唯一标识：由进程ID和主机名组成和一个随机UUID组成。
     *
     * @return 返回唯一标识符字符串，例如 "12345@localhost"
     */
    public static String getProcessIdentifier() {
        return getHostName() + "@" + getProcessId() + "@" + UUID;
    }

    /**
     * 获取当前进程ID，使用缓存
     *
     * @return 返回当前进程的ID，例如 "12345"
     */
    public static String getProcessId() {
        if (cachedProcessId == null) {
            String jvmName = ManagementFactory.getRuntimeMXBean().getName();
            cachedProcessId = jvmName.split("@")[0]; // 缓存进程ID
        }
        return cachedProcessId;
    }

    /**
     * 获取当前主机名，使用缓存
     *
     * @return 返回当前主机名，例如 "localhost"
     */
    public static String getHostName() {
        if (cachedHostName == null) {
            try {
                cachedHostName = InetAddress.getLocalHost().getHostName(); // 缓存主机名
            } catch (Exception e) {
                cachedHostName = "unknown";
            }
        }
        return cachedHostName;
    }

    /**
     * 获取主机的默认IP地址，使用缓存
     *
     * @return 返回主机的默认IP地址
     */
    public static String getDefaultIpAddress() {
        if (cachedDefaultIpAddress == null) {
            try {
                cachedDefaultIpAddress = InetAddress.getLocalHost().getHostAddress(); // 缓存默认IP地址
            } catch (Exception e) {
                cachedDefaultIpAddress = "unknown";
            }
        }
        return cachedDefaultIpAddress;
    }

    /**
     * 获取指定网卡的IP地址，使用缓存
     *
     * @param networkInterfaceName 网卡名称，例如 "eth0"、"en0" 或者 "wlan0"
     * @return 返回指定网卡的IP地址，如果找不到则返回 "unknown"
     */
    public static String getIpAddressByNetworkInterface(String networkInterfaceName) {
        if (cachedNetworkInterfaceIpAddress == null) {
            try {
                NetworkInterface networkInterface = NetworkInterface.getByName(networkInterfaceName);
                if (networkInterface != null) {
                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress inetAddress = addresses.nextElement();
                        if (!inetAddress.isLoopbackAddress() && inetAddress instanceof java.net.Inet4Address) {
                            cachedNetworkInterfaceIpAddress = inetAddress.getHostAddress(); // 缓存网卡IP地址
                            break;
                        }
                    }
                }
            } catch (SocketException e) {
                cachedNetworkInterfaceIpAddress = "unknown";
            }
        }
        return cachedNetworkInterfaceIpAddress != null ? cachedNetworkInterfaceIpAddress : "unknown";
    }

}
